package buaa.qel.rpc.client.handler;

import buaa.qel.rpc.common.codec.RpcMessageCodec;
import buaa.qel.rpc.common.message.Beat;
import buaa.qel.rpc.common.protocol.RpcProtocolFrameDecoder;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.timeout.IdleStateHandler;

import java.util.concurrent.TimeUnit;

public class RpcClientChannelInitializer extends ChannelInitializer<SocketChannel> {
    LoggingHandler LOGGING_HANDLER = new LoggingHandler(LogLevel.DEBUG);
    RpcMessageCodec MESSAGE_CODEC = new RpcMessageCodec();
    RpcClientHandler RPC_HANDLER = new RpcClientHandler();

    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        ChannelPipeline cp = socketChannel.pipeline();
        cp.addLast(new IdleStateHandler(0, 0, Beat.BEAT_INTERVAL, TimeUnit.SECONDS));
        cp.addLast(new RpcProtocolFrameDecoder());
        cp.addLast(LOGGING_HANDLER);
        cp.addLast(MESSAGE_CODEC);
        cp.addLast(RPC_HANDLER);
    }
}
